<template>
  <view>
    <view class="goods-list">
      <view v-for="(item,index) in goodsList" :key="index" @click="gotoDetail(item)">
        <my-goods :item="item"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [],
        total: 0,
        isLoading:false
      };
    },
    onLoad(options) {
      this.queryObj.cid = options.cid || ''
      this.queryObj.query = options.query || ''
      this.getGoodsList()

    },
    onReachBottom() {
      // 当数据加载完毕提醒
       if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕！')
      // 如果正在请求数据节流阀开启则不在触发触底
      if(this.isLoading) return
      this.queryObj.pagenum+=1
      this.getGoodsList()
    },
    onPullDownRefresh() {
      // 下拉刷新将页数等清零
      this.queryObj.pagenum=1
      this.total = 0
      this.isloading = false
      this.goodsList = []
      // 在重新调取数据
      this.getGoodsList(()=>uni.stopPullDownRefresh())
      
    },
    methods: {
      async getGoodsList(cb) {
        // 开启节流阀
        this.isLoading=true
        const {
          data: res
        } = await uni.$http.get("/api/public/v1/goods/search", this.queryObj)
        cb && cb()
        if (res.meta.status !== 200) return uni.$showMsg()
        this.goodsList =[...this.goodsList,...res.message.goods] 
        this.total = res.message.total
        // 关闭节流阀
        this.isLoading=false
      },
        // 跳转商品详情页面
      gotoDetail(item){
        uni.navigateTo({
            url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
          })
      }
    }

  }
</script>

<style lang="scss">
 
</style>
